package com.ruoyi.StarStudio.Service.Impl;

import com.ruoyi.StarStudio.Domain.Meteorology;
import com.ruoyi.StarStudio.Mapper.MeteorologyMapper;
import com.ruoyi.StarStudio.Mapper.ZoneMapper;
import com.ruoyi.StarStudio.Service.MeteorologyService;
import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class MeteorologyServiceImpl implements MeteorologyService {

    @Autowired
    private MeteorologyMapper meteorologyMapper;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ZoneMapper zoneMapper;

    @Override
    public List<Meteorology> findAll() {
        return meteorologyMapper.findAll();
    }

    @Override
    public Meteorology getByMeteId(long meteorology_id) {
        return meteorologyMapper.getByMeteId(meteorology_id);
    }

    /**
     * 根据地块ID获取全部数据
     * @param zoneId 地块ID
     * @return 气象信息集合
     */
    @Override
    public List<Meteorology> getAllByZoneId(int zoneId) {
        return meteorologyMapper.getAllByZoneId(zoneId);
    }

    /**
     * 获取最新插入的一条数据
     * @param zoneId 地块ID
     * @return 气象信息对象
     */
    @Override
    public Meteorology getOneByZoneId(int zoneId) {
        long meteId = meteorologyMapper.findMaxMeteIdFromZone_mete(zoneId);
        return meteorologyMapper.getOneByZoneId(meteId);
    }

    /**
     * 新增气象数据
     * @param meteorology 气象信息对象
     */
    @Override
    public void add(Meteorology meteorology,int zoneId) {
        meteorologyMapper.add(meteorology);
        meteorologyMapper.addToZone_Meteorology(zoneId,meteorology.getMeteorology_id());
    }

    @Override
    public void update(Meteorology meteorology) {
        meteorologyMapper.update(meteorology);
    }

    @Override
    public void delete(long meteorologyId) {
        meteorologyMapper.deleteFromZone_Meteorology(meteorologyId);
        meteorologyMapper.delete(meteorologyId);
    }

    /**
     * 带搜索查询
     * @param meteorology 气象信息对象
     */
    @Override
    public List<Meteorology> getList(Meteorology meteorology) {
        return meteorologyMapper.getList(meteorology);
    }

    /**
     * 从 Redis中获取气象相关的传感器数据，保存到数据库中
     */
    public void SensorAdd() {
        new Thread(()->{
            List<Integer> zoneIdList = zoneMapper.getZoneIdList();
            while (true) {
                for (Integer zoneID : zoneIdList) {
                    Double AirTemperature = redisCache.getCacheObject("AirTemperature_"+zoneID);
                    Double AirHumidity = redisCache.getCacheObject("AirHumidity_"+zoneID);
                    Double Illuminance = redisCache.getCacheObject("Illuminance_"+zoneID);
                    Double Precipitation = redisCache.getCacheObject("Precipitation_"+zoneID);
                    Double CO2 = redisCache.getCacheObject("CO2_"+zoneID);
                    Double WindDir = redisCache.getCacheObject("WindDir_"+zoneID);
                    Double WindSpeed = redisCache.getCacheObject("WindSpeed_"+zoneID);
                    if (AirTemperature != null && AirHumidity != null && Illuminance != null && CO2 != null && WindDir != null && WindSpeed != null){
                        Meteorology meteorology = new Meteorology();
                        meteorology.setMeteorology_temperature(AirTemperature);
                        meteorology.setMeteorology_humidity(AirHumidity);
                        meteorology.setMeteorology_co2(CO2);
                        meteorology.setMeteorology_illuminance(Illuminance);
                        meteorology.setMeteorology_rainfall(Precipitation);
                        meteorology.setMeteorology_wind_direction(WindDir);
                        meteorology.setMeteorology_wind_speed(WindSpeed);
                        meteorology.setMeteorology_updateTime(new Timestamp(System.currentTimeMillis()));
                        meteorologyMapper.add(meteorology);
                        meteorologyMapper.addToZone_Meteorology(zoneID, meteorology.getMeteorology_id());
                    }
                }
                try {
                    //每隔 5s 从 Redis中获取数据并存储
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }

    /**
     * 定时任务   每天00:00:00清除最早一天的气象数据 （ 17280条）
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void AutoDeleteMeteData(){
        List<Long> autoDeleteMeteIds = meteorologyMapper.findAutoDeleteMeteIds();
        if (autoDeleteMeteIds.size() > 0){
            for (Long autoDeleteMeteId : autoDeleteMeteIds) {
                meteorologyMapper.delete(autoDeleteMeteId);
                meteorologyMapper.deleteFromZone_Meteorology(autoDeleteMeteId);
            }
        }
    }
}
